package cn.itcast.b_dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


/**
 * Created by 13687 on 2017/2/26.
 * 动态创建
 */
public class ProxyDynamic {
	// 泛化
	private Object target;

	public ProxyDynamic(Object target) {
		this.target = target;
	}

	// 给目标对象，生成代理对象
	public Object getProxyInstance(){
		return Proxy.newProxyInstance(target.getClass().getClassLoader(),
				target.getClass().getInterfaces(),
				new InvocationHandler() {
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						// 对目标对象的方法进行包装
						System.out.println("开启事务");
						Object returnValue = method.invoke(target, args);
						System.out.println("结束事务");
						return returnValue;
					}
				});
	}
}
